package com.szzg10.mina2.chapter3;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;

import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

public class TcpServer {

	public static final int PORT = 9123;
	
	public static void main(String[] args) throws IOException, InterruptedException {
		
		NioSocketAcceptor acceptor = new NioSocketAcceptor();
		
		acceptor.getFilterChain().addLast("logger", new LoggingFilter());
		acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
		
		acceptor.setHandler(new TcpServerHandler());
		
		acceptor.bind(new InetSocketAddress(PORT));
		
		System.out.println("isActive: " + acceptor.isActive());
		System.out.println("isDisposing: " + acceptor.isDisposing());
		System.out.println("isDisposed: " + acceptor.isDisposed());
		
//		acceptor.dispose(true);
			
	}
}
